package com.lt.report.controller.doubleController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lt.report.bean.doubleBean.CityConfirmInfo;
import com.lt.report.bean.doubleBean.TdMStaff;
import com.lt.report.controller.common.TableResponseVO;
import com.lt.report.service.doubleService.DoubleService;
import com.lt.report.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class DoubleController{

    /**
     * 根据角色获取表单
     * @param
     * @return
     */
    @CrossOrigin
    @PostMapping("doubleList")
    @ResponseBody
    public TableResponseVO getTableByRole(@RequestBody String params){
        try {

            log.info("params###getTableByRole"+params);
            //测试数据旧 {"city":"473","business":"3","mouth":"201901","staffId":"YWM00306","page":1,"eparchyCode":"0474","departId":"29F51"}"
            //测试数据新 {"city":"","business":"","mouth":"","staffId":"YWM00306","page":1,"size":12,"eparchyCode":"0474","departId":"29F51","roleType":"0"}
            JSONObject jsonObject = JSON.parseObject(params);
            Map<String,Object> map = new HashMap<>();
            //地市
            map.put("eparchyCode",jsonObject.getString("eparchyCode"));
            //区公司搜索条件
            map.put("city",jsonObject.getString("city"));
            //账期
            map.put("mouth",jsonObject.getString("mouth"));
            //部门
            map.put("departId",jsonObject.getString("departId"));
            //业务类型
            map.put("business",jsonObject.getString("business"));
            map.put("page",jsonObject.getString("page"));
            map.put("business",jsonObject.getString("business"));
            //roleType 0 管理员 1 地市人员
            map.put("roleType",jsonObject.getString("roleType"));
            Integer page = jsonObject.getInteger("page");
            Integer size = jsonObject.getInteger("size");
            map.put("pageSizeEnd",page*size);
            map.put("pageSizeStart",page*size-size);
            return  doubleService.queryBySearchMap(map);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Autowired
    DoubleService doubleService;
    @CrossOrigin
    @GetMapping("getRole")
    @ResponseBody
    public TdMStaff queryRole(@RequestParam("staffId") String staffId){
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("staffId",staffId);
            TdMStaff role =doubleService.queryRole(map);
            return role;
        }catch (Exception e){
            e.printStackTrace();
            return null;//暫無用戶非法登錄

        }
    }
    /*
    插入双计实体
     */
    @CrossOrigin
    @PostMapping("insertCityConfirmInfo")
    @ResponseBody
    public ResponseResult insert(@RequestBody String params){
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            //双计收入
            BigDecimal financialIncome = new BigDecimal(jsonObject.getString("financialIncome"));
            BigDecimal doubleIncome = new BigDecimal(jsonObject.getString("doubleIncome"));
            BigDecimal finaloutcome = financialIncome.add(doubleIncome);
            jsonObject.put("financialIncome", financialIncome);
            jsonObject.put("doubleIncome", doubleIncome);
            jsonObject.put("finalOutcome",finaloutcome);
            params = jsonObject.toJSONString();
            CityConfirmInfo cityConfirmInfo = JSON.parseObject(params,CityConfirmInfo.class);
            int count =doubleService.insert(cityConfirmInfo);
            if (count>0){
                return new ResponseResult(true,"成功");
            }
            return new ResponseResult(false,"插入失败");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(false,"插入失败");
        }
    }

    /*
    根据ID查详细信息
     */
    @CrossOrigin
    @GetMapping("getById")
    @ResponseBody
    public CityConfirmInfo selectByPrimaryKey(@RequestParam("id") String id) {
        try {
            CityConfirmInfo cityConfirmInfo= doubleService.selectByPrimaryKey(id);
            return cityConfirmInfo;
        }catch (Exception e){
            e.printStackTrace();
            return new CityConfirmInfo();
        }
    }

    /*
    根据ID更新信息
     */
    @CrossOrigin
    @PostMapping("updateCityConfirmInfoById")
    @ResponseBody
    public ResponseResult updateCityConfirmInfoById(@RequestBody String params) {
        try {
            JSONObject jsonObject = JSON.parseObject(params);
            BigDecimal financialIncome = new BigDecimal(jsonObject.getString("financialIncome"));
            BigDecimal doubleIncome = new BigDecimal(jsonObject.getString("doubleIncome"));
            BigDecimal finaloutcome = financialIncome.add(doubleIncome);
            jsonObject.put("financialIncome", financialIncome);
            jsonObject.put("doubleIncome", doubleIncome);
            jsonObject.put("finalOutcome",finaloutcome);
            params = jsonObject.toJSONString();
            CityConfirmInfo cityConfirmInfo = JSON.parseObject(params,CityConfirmInfo.class);
            int count = doubleService.updateByPrimaryKeySelective(cityConfirmInfo);
            return new ResponseResult(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false, "插入失败");
        }
    }


    /*
    根据ID 确认信息
     */

    @CrossOrigin
    @PostMapping("confirmItem")
    @ResponseBody
    public ResponseResult confirmItem(@RequestBody String params) {
        try {
           log.info("确认数据 参数是："+params);
            CityConfirmInfo cityConfirmInfo = JSON.parseObject(params,CityConfirmInfo.class);
            int count = doubleService.updateConfirm(cityConfirmInfo);
            return new ResponseResult(true, "确认成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false, "确认失败");
        }
    }


    /*
   根据ID删除信息
    */

    @CrossOrigin
    @DeleteMapping("deleteCityConfirmInfoById")
    @ResponseBody
    public ResponseResult deleteCityConfirmInfoById(@RequestParam("id") String id) {
        try {
            int count =doubleService.deleteByPrimaryKey(id);
            return new ResponseResult(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(false,"删除失败");
        }
    }





}
